#!/usr/bin/env python
# coding: utf-8


import pymongo
import logging
from pymongo import errors
from config.config import configs
from utils.mongodbutils import MongodbUtils


class MongodbByPwdUtils(MongodbUtils):
    """
    Mongodb 工具类
    """

    conf = configs["mongodb_user"]

    @classmethod
    def conn(cls):
        """
        connect to mongodb
        :return:
        """

        db = None
        try:
            client = pymongo.MongoClient(cls.conf["host"], cls.conf["port"], appname='zhangbc')
            db = client[cls.conf["db"]]
            db.authenticate(cls.conf["username"], cls.conf["pwd"])
        except pymongo.errors.OperationFailure as ex:
            logging.error('Database connect refuse. reason: {0}'.format(ex))
        return db


if __name__ == '__main__':

    MongodbByPwdUtils.get_records('arr_shenzhen_market', {"stock_date": "2013-05-02"})
